package com.qdxwx.manage.controller.book;

import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.book.BookService;
import com.qdxwx.data.service.channel.ChannelService;
import com.qdxwx.data.service.teacher.TeacherService;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.book.Book;
import com.qdxwx.models.book.BookQuery;
import com.qdxwx.models.channel.Channel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 书籍Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/book")
public class BookController extends BaseController {

    @Autowired
    private BookService bookService;
    @Autowired
    private ChannelService channelService;
    @Autowired
    private TeacherService teacherService;

    @Permissions("book:list")
    @GetMapping("/list.html")
    public String list(HttpServletRequest request, ModelMap mmap) {
        int channelId = TKConvert.toInteger(request.getParameter("channelId"));
        Channel channel = new Channel();
        channel.setModuleId(3);
        channel.setIsDisabled(0);
        List<Channel> channelList = channelService.getList(channel, null);
        if (channelId < 1 && channelList.size() > 0) {
            channelId = channelList.get(0).getChannelId();
        }
        mmap.put("channelId", channelId);
        mmap.put("channelList", channelList);
        return "book/book-list";
    }

    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        int channelId = TKConvert.toInteger(request.getParameter("channelId"));
        String searchKey = request.getParameter("searchKey");
        if (TKConvert.toInteger(searchKey) > 0) {
            Book book = bookService.get(TKConvert.toInteger(searchKey));
            if (book == null) return TableDataInfo.error("找不到指定id的书籍");
            List<Book> list = new ArrayList<>();
            list.add(book);
            return TableDataInfo.success(list, 1);
        }
        //Integer isAvailable = TKConvert.toInteger(request.getParameter("isAvailable"), null);
        BookQuery query = new BookQuery();
        query.setChannelId(channelId);
        query.setTitle(TKString.isNotEmpty(searchKey) ? searchKey : null);
        //query.setIsAvailable(isAvailable);
        List<Book> list = bookService.getList(query, PageRequest.form(request));
        int count = bookService.count(query);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(count);
        return rspData;
    }

    @GetMapping("/view.html")
    public String view(HttpServletRequest request, ModelMap mmap) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        if (id < 1) {
            return error404;
        }
        Book book = bookService.get(id);
        if (book == null) {
            return error404;
        }
        mmap.put("book", book);
        mmap.put("channel", channelService.get(book.getChannelId()));
        mmap.put("teachers", teacherService.getListByBookId(book.getId()));
        return "book/book-view";
    }

}
